home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / zfilterx.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  9.0 KB  |  330 lines

  1. /* Copyright (C) 1995, 1996, 1998, 1999 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: zfilterx.c,v 1.2 2000/09/19 19:00:53 lpd Exp $ */
  20. /* Extended (non-standard) filter creation */
  21. #include "memory_.h"
  22. #include "ghost.h"
  23. #include "oper.h"
  24. #include "gsstruct.h"
  25. #include "ialloc.h"
  26. #include "idict.h"
  27. #include "idparam.h"
  28. #include "store.h"
  29. #include "strimpl.h"
  30. #include "sfilter.h"
  31. #include "sbwbs.h"
  32. #include "sbhc.h"
  33. #include "sbtx.h"
  34. #include "shcgen.h"
  35. #include "smtf.h"
  36. #include "ifilter.h"
  37.  
  38. /* ------ Bounded Huffman code filters ------ */
  39.  
  40. /* Common setup for encoding and decoding filters */
  41. private int
  42. bhc_setup(os_ptr op, stream_BHC_state * pbhcs)
  43. {
  44.     int code;
  45.     int num_counts;
  46.     int data[max_hc_length + 1 + 256 + max_zero_run + 1];
  47.     uint dsize;
  48.     int i;
  49.     uint num_values, accum;
  50.     ushort *counts;
  51.     ushort *values;
  52.  
  53.     check_type(*op, t_dictionary);
  54.     check_dict_read(*op);
  55.     if ((code = dict_bool_param(op, "FirstBitLowOrder", false,
  56.                 &pbhcs->FirstBitLowOrder)) < 0 ||
  57.     (code = dict_int_param(op, "MaxCodeLength", 1, max_hc_length,
  58.                    max_hc_length, &num_counts)) < 0 ||
  59.     (code = dict_bool_param(op, "EndOfData", true,
  60.                 &pbhcs->EndOfData)) < 0 ||
  61.     (code = dict_uint_param(op, "EncodeZeroRuns", 2, 256,
  62.                 256, &pbhcs->EncodeZeroRuns)) < 0 ||
  63.     /* Note: the code returned from the following call */
  64.     /* is actually the number of elements in the array. */
  65.     (code = dict_int_array_param(op, "Tables", countof(data),
  66.                      data)) <= 0
  67.     )
  68.     return (code < 0 ? code : gs_note_error(e_rangecheck));
  69.     dsize = code;
  70.     if (dsize <= num_counts + 2)
  71.     return_error(e_rangecheck);
  72.     for (i = 0, num_values = 0, accum = 0; i <= num_counts;
  73.      i++, accum <<= 1
  74.     ) {
  75.     int count = data[i];
  76.  
  77.     if (count < 0)
  78.         return_error(e_rangecheck);
  79.     num_values += count;
  80.     accum += count;
  81.     }
  82.     if (dsize != num_counts + 1 + num_values ||
  83.     accum != 1 << (num_counts + 1) ||
  84.     pbhcs->EncodeZeroRuns >
  85.     (pbhcs->EndOfData ? num_values - 1 : num_values)
  86.     )
  87.     return_error(e_rangecheck);
  88.     for (; i < num_counts + 1 + num_values; i++) {
  89.     int value = data[i];
  90.  
  91.     if (value < 0 || value >= num_values)
  92.         return_error(e_rangecheck);
  93.     }
  94.     pbhcs->definition.counts = counts =
  95.     (ushort *) ialloc_byte_array(num_counts + 1, sizeof(ushort),
  96.                      "bhc_setup(counts)");
  97.     pbhcs->definition.values = values =
  98.     (ushort *) ialloc_byte_array(num_values, sizeof(ushort),
  99.                      "bhc_setup(values)");
  100.     if (counts == 0 || values == 0) {
  101.     ifree_object(values, "bhc_setup(values)");
  102.     ifree_object(counts, "bhc_setup(counts)");
  103.     return_error(e_VMerror);
  104.     }
  105.     for (i = 0; i <= num_counts; i++)
  106.     counts[i] = data[i];
  107.     pbhcs->definition.counts = counts;
  108.     pbhcs->definition.num_counts = num_counts;
  109.     for (i = 0; i < num_values; i++)
  110.     values[i] = data[i + num_counts + 1];
  111.     pbhcs->definition.values = values;
  112.     pbhcs->definition.num_values = num_values;
  113.     return 0;
  114. }
  115.  
  116. /* <target> <dict> BoundedHuffmanEncode/filter <file> */
  117. private int
  118. zBHCE(i_ctx_t *i_ctx_p)
  119. {
  120.     os_ptr op = osp;
  121.     stream_BHCE_state bhcs;
  122.     int code = bhc_setup(op, (stream_BHC_state *)&bhcs);
  123.  
  124.     if (code < 0)
  125.     return code;
  126.     return filter_write(op, 0, &s_BHCE_template, (stream_state *)&bhcs, 0);
  127. }
  128.  
  129. /* <source> <dict> BoundedHuffmanDecode/filter <file> */
  130. private int
  131. zBHCD(i_ctx_t *i_ctx_p)
  132. {
  133.     os_ptr op = osp;
  134.     stream_BHCD_state bhcs;
  135.     int code = bhc_setup(op, (stream_BHC_state *)&bhcs);
  136.  
  137.     if (code < 0)
  138.     return code;
  139.     return filter_read(i_ctx_p, 0, &s_BHCD_template, (stream_state *)&bhcs, 0);
  140. }
  141.  
  142. /* <array> <max_length> .computecodes <array> */
  143. /* The first max_length+1 elements of the array will be filled in with */
  144. /* the code counts; the remaining elements will be replaced with */
  145. /* the code values.  This is the form needed for the Tables element of */
  146. /* the dictionary parameter for the BoundedHuffman filters. */
  147. private int
  148. zcomputecodes(i_ctx_t *i_ctx_p)
  149. {
  150.     os_ptr op = osp;
  151.     os_ptr op1 = op - 1;
  152.     uint asize;
  153.     hc_definition def;
  154.     ushort *data;
  155.     long *freqs;
  156.     int code = 0;
  157.  
  158.     check_type(*op, t_integer);
  159.     check_write_type(*op1, t_array);
  160.     asize = r_size(op1);
  161.     if (op->value.intval < 1 || op->value.intval > max_hc_length)
  162.     return_error(e_rangecheck);
  163.     def.num_counts = op->value.intval;
  164.     if (asize < def.num_counts + 2)
  165.     return_error(e_rangecheck);
  166.     def.num_values = asize - (def.num_counts + 1);
  167.     data = (ushort *) gs_alloc_byte_array(imemory, asize, sizeof(ushort),
  168.                       "zcomputecodes");
  169.     freqs = (long *)gs_alloc_byte_array(imemory, def.num_values,
  170.                     sizeof(long),
  171.                     "zcomputecodes(freqs)");
  172.  
  173.     if (data == 0 || freqs == 0)
  174.     code = gs_note_error(e_VMerror);
  175.     else {
  176.     uint i;
  177.  
  178.     def.counts = data;
  179.     def.values = data + (def.num_counts + 1);
  180.     for (i = 0; i < def.num_values; i++) {
  181.         const ref *pf = op1->value.const_refs + i + def.num_counts + 1;
  182.  
  183.         if (!r_has_type(pf, t_integer)) {
  184.         code = gs_note_error(e_typecheck);
  185.         break;
  186.         }
  187.         freqs[i] = pf->value.intval;
  188.     }
  189.     if (!code) {
  190.         code = hc_compute(&def, freqs, imemory);
  191.         if (code >= 0) {
  192.         /* Copy back results. */
  193.         for (i = 0; i < asize; i++)
  194.             make_int(op1->value.refs + i, data[i]);
  195.         }
  196.     }
  197.     }
  198.     gs_free_object(imemory, freqs, "zcomputecodes(freqs)");
  199.     gs_free_object(imemory, data, "zcomputecodes");
  200.     if (code < 0)
  201.     return code;
  202.     pop(1);
  203.     return code;
  204. }
  205.  
  206. /* ------ Burrows/Wheeler block sorting filters ------ */
  207.  
  208. /* Common setup for encoding and decoding filters */
  209. private int
  210. bwbs_setup(os_ptr op, stream_BWBS_state * pbwbss)
  211. {
  212.     int code =
  213.     dict_int_param(op, "BlockSize", 1, max_int / sizeof(int) - 10, 16384,
  214.                &pbwbss->BlockSize);
  215.  
  216.     if (code < 0)
  217.     return code;
  218.     return 0;
  219. }
  220.  
  221. /* <target> <dict> BWBlockSortEncode/filter <file> */
  222. private int
  223. zBWBSE(i_ctx_t *i_ctx_p)
  224. {
  225.     os_ptr op = osp;
  226.     stream_BWBSE_state bwbss;
  227.     int code;
  228.  
  229.     check_type(*op, t_dictionary);
  230.     check_dict_read(*op);
  231.     code = bwbs_setup(op, (stream_BWBS_state *)&bwbss);
  232.     if (code < 0)
  233.     return code;
  234.     return filter_write(op, 0, &s_BWBSE_template, (stream_state *)&bwbss, 0);
  235. }
  236.  
  237. /* <source> <dict> BWBlockSortDecode/filter <file> */
  238. private int
  239. zBWBSD(i_ctx_t *i_ctx_p)
  240. {
  241.     os_ptr op = osp;
  242.     stream_BWBSD_state bwbss;
  243.     int code = bwbs_setup(op, (stream_BWBS_state *)&bwbss);
  244.  
  245.     if (code < 0)
  246.     return code;
  247.     return filter_read(i_ctx_p, 0, &s_BWBSD_template, (stream_state *)&bwbss, 0);
  248. }
  249.  
  250. /* ------ Byte translation filters ------ */
  251.  
  252. /* Common setup */
  253. private int
  254. bt_setup(os_ptr op, stream_BT_state * pbts)
  255. {
  256.     check_read_type(*op, t_string);
  257.     if (r_size(op) != 256)
  258.     return_error(e_rangecheck);
  259.     memcpy(pbts->table, op->value.const_bytes, 256);
  260.     return 0;
  261. }
  262.  
  263. /* <target> <table> ByteTranslateEncode/filter <file> */
  264. /* <target> <table> <dict> ByteTranslateEncode/filter <file> */
  265. private int
  266. zBTE(i_ctx_t *i_ctx_p)
  267. {
  268.     os_ptr op = osp;
  269.     stream_BT_state bts;
  270.     int code = bt_setup(op, &bts);
  271.  
  272.     if (code < 0)
  273.     return code;
  274.     return filter_write(op, 0, &s_BTE_template, (stream_state *)&bts, 0);
  275. }
  276.  
  277. /* <target> <table> ByteTranslateDecode/filter <file> */
  278. /* <target> <table> <dict> ByteTranslateDecode/filter <file> */
  279. private int
  280. zBTD(i_ctx_t *i_ctx_p)
  281. {
  282.     os_ptr op = osp;
  283.     stream_BT_state bts;
  284.     int code = bt_setup(op, &bts);
  285.  
  286.     if (code < 0)
  287.     return code;
  288.     return filter_read(i_ctx_p, 0, &s_BTD_template, (stream_state *)&bts, 0);
  289. }
  290.  
  291. /* ------ Move-to-front filters ------ */
  292.  
  293. /* <target> MoveToFrontEncode/filter <file> */
  294. /* <target> <dict> MoveToFrontEncode/filter <file> */
  295. private int
  296. zMTFE(i_ctx_t *i_ctx_p)
  297. {
  298.     os_ptr op = osp;
  299.  
  300.     return filter_write_simple(op, &s_MTFE_template);
  301. }
  302.  
  303. /* <source> MoveToFrontDecode/filter <file> */
  304. /* <source> <dict> MoveToFrontDecode/filter <file> */
  305. private int
  306. zMTFD(i_ctx_t *i_ctx_p)
  307. {
  308.     os_ptr op = osp;
  309.  
  310.     return filter_read_simple(op, &s_MTFD_template);
  311. }
  312.  
  313. /* ================ Initialization procedure ================ */
  314.  
  315. const op_def zfilterx_op_defs[] =
  316. {
  317.     {"2.computecodes", zcomputecodes},    /* not a filter */
  318.     op_def_begin_filter(),
  319.         /* Non-standard filters */
  320.     {"2BoundedHuffmanEncode", zBHCE},
  321.     {"2BoundedHuffmanDecode", zBHCD},
  322.     {"2BWBlockSortEncode", zBWBSE},
  323.     {"2BWBlockSortDecode", zBWBSD},
  324.     {"2ByteTranslateEncode", zBTE},
  325.     {"2ByteTranslateDecode", zBTD},
  326.     {"1MoveToFrontEncode", zMTFE},
  327.     {"1MoveToFrontDecode", zMTFD},
  328.     op_def_end(0)
  329. };
  330.